package com.shuai.analysis.controller;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.shuai.analysis.domain.entity.ChartInfo;
import com.shuai.analysis.domain.request.ChartAlyRequest;
import com.shuai.analysis.domain.request.ChartQueryRequest;
import com.shuai.analysis.domain.vo.ChartInfoVO;
import com.shuai.analysis.service.ChartInfoService;
import com.shuai.common.constant.AuthenticationConstant;
import com.shuai.common.constant.ErrorCode;
import com.shuai.common.exception.BusinessException;
import com.shuai.common.rps.BaseResponse;
import com.shuai.common.utils.MyJWTUtil;
import com.shuai.common.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

@RestController
@RequestMapping("/chart")
@Validated
public class ChartController {

    @Autowired
    private ChartInfoService chartInfoService;

    @PostMapping("/async/aly")
    public BaseResponse<Boolean> analysis(@RequestPart("file") MultipartFile file,
                                          @RequestHeader(AuthenticationConstant.AUTHENTICATION_HEADER) String auth,
                                          @Valid ChartAlyRequest chartAlyRequest) {
        String userId = (String) MyJWTUtil.getPayLoadItem(auth, AuthenticationConstant.PAYLOAD_USERID);
        if (CharSequenceUtil.isBlank(userId)) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR, "登录状态过期，请重新登录");
        }
        boolean isSubmit = chartInfoService.analysis(file, userId, chartAlyRequest);
        if (!isSubmit) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "提交失败，请稍后重试");
        }
        return ResultUtils.success(Boolean.TRUE);
    }

    @PostMapping("list")
    public BaseResponse<PageInfo<ChartInfoVO>> list(@RequestBody @Valid ChartQueryRequest chartQueryRequest,
                                                    @RequestHeader(AuthenticationConstant.AUTHENTICATION_HEADER) String auth) {
        String userId = (String) MyJWTUtil.getPayLoadItem(auth, AuthenticationConstant.PAYLOAD_USERID);
        if (CharSequenceUtil.isBlank(userId)) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR, "登录状态过期，请重新登录");
        }
        PageInfo<ChartInfoVO> chartInfoVOPage = chartInfoService.conditionList(chartQueryRequest, userId);
        return ResultUtils.success(chartInfoVOPage);
    }

    @GetMapping("/del}")
    public BaseResponse<Boolean> del(@RequestParam("id") Long id,
                                     @RequestHeader(AuthenticationConstant.AUTHENTICATION_HEADER) String auth) {
        String userId = (String) MyJWTUtil.getPayLoadItem(auth, AuthenticationConstant.PAYLOAD_USERID);
        if (CharSequenceUtil.isBlank(userId)) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR, "登录状态过期，请重新登录");
        }
        LambdaQueryWrapper<ChartInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChartInfo::getId, id);
        queryWrapper.eq(ChartInfo::getUserId, userId);
        if (!chartInfoService.remove(queryWrapper)) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "删除失败，请稍后重试");
        }
        return ResultUtils.success(Boolean.TRUE);
    }
}
